﻿using MyLiEda.Events.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace MyLiEda.Api
{
    public class MyLiEventHandler : IEventHandler<MyLiEvent>
    {
        private IEventStore _eventStore;

        public MyLiEventHandler(IEventStore eventStore)
        {
            _eventStore = eventStore;
        }

        public bool CanHandle(IEvent @event)
        => @event.GetType().Equals(typeof(MyLiEvent));

        public async Task<bool> HandleAsync(MyLiEvent @event, CancellationToken cancellationToken = default)
        {
            //Console.WriteLine($"MyLiEvent name is {@event.MyName}");
            await this._eventStore.SaveEventAsync(@event);
            return true;
        }

        public Task<bool> HandleAsync(IEvent @event, CancellationToken cancellationToken = default)
        {
            return CanHandle(@event) ? HandleAsync((MyLiEvent)@event, cancellationToken):Task.FromResult(false);
        }
    }
}
